package com.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;

import com.bean.OrderSave;
import com.bean.OrderCreate;
import com.bean.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.utils.ChgEcod;
import com.utils.HibCommons;
import com.utils.RenameImg;

public class AdminOrderAction extends ActionSupport {
	final static String PATH = ServletActionContext.getServletContext().getRealPath("/");
	private OrderCreate order = new OrderCreate();
	private OrderSave order_s = new OrderSave();
	private User user = new User();
	private List<OrderCreate> admin_order;
	private List<OrderSave> order_save;
	private File order_add;
	private String order_addFileName;
	
	/* SongCreate分页方法 */
	private int pageNum_sc;// 当前页
	private int totalpage_sc;// 总页数
	private int pageSize_sc = 3;// 每页条目数
	private int Amount_sc;// 总条目数

	/* SongSave分页方法 */
	private int pageNum_ss;// 当前页
	private int totalpage_ss;// 总页数
	private int pageSize_ss = 3;// 每页条目数
	private int Amount_ss;// 总条目数

	public String my_order() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String image = request.getParameter("image");
		username = new String(username.getBytes("ISO8859-1"), "UTF-8");
		

		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);
		return SUCCESS;
	}

	public String my_order_create() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		String username = ChgEcod.change(request.getParameter("username"));
		String image = request.getParameter("image");
		
		/* 须加分页功能 */
		Amount_sc = getAmount_sc(username);
		this.totalpage_sc = Amount_sc % pageSize_sc == 0 ? (Amount_sc / pageSize_sc) : (Amount_sc / pageSize_sc + 1);
		if (pageNum_sc <= 0)
			this.pageNum_sc = 1;
		if (pageNum_sc > totalpage_sc)
			this.pageNum_sc = totalpage_sc;
		HibCommons.OpenQuery();
		String hql_crea = "FROM OrderCreate o where o.order_creater = :username";
		Query query_crea = HibCommons.getSession().createQuery(hql_crea);
		query_crea.setParameter("username", username);
		query_crea.setFirstResult(((pageNum_sc - 1) * pageSize_sc));
		query_crea.setMaxResults(pageSize_sc);
		admin_order = query_crea.list();
		HibCommons.CloseQuery();

		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);
		return SUCCESS;
	}

	public String my_order_save() throws Exception {

		HttpServletRequest request = ServletActionContext.getRequest();
		String username = ChgEcod.change(request.getParameter("username"));
		String image = request.getParameter("image");
		
		/* 须加分页功能 */
		Amount_ss = getAmount_ss(username);
		this.totalpage_ss = Amount_ss % pageSize_ss == 0 ? (Amount_ss / pageSize_ss) : (Amount_ss / pageSize_ss + 1);
		if (pageNum_ss <= 0)
			this.pageNum_ss = 1;
		if (pageNum_ss > totalpage_ss)
			this.pageNum_ss = totalpage_ss;
		
		HibCommons.OpenQuery();
		String hql_save = "FROM OrderSave o where o.order_saver = :username";
		Query query_save = HibCommons.getSession().createQuery(hql_save);
		query_save.setParameter("username", username);
		query_save.setFirstResult(((pageNum_ss - 1) * pageSize_ss));
		query_save.setMaxResults(pageSize_ss);

		order_save = query_save.list();
		HibCommons.CloseQuery();

		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);
		return SUCCESS;
	}

	public String add_songorder() throws Exception {
		/* 文件上传 */
		InputStream is = new FileInputStream(order_add);
		String order_img = RenameImg.getNewName(order_addFileName);
		OutputStream os = new FileOutputStream(PATH + "img/" + order_img);
		byte buffer[] = new byte[1024];
		int count = 0;
		while ((count = is.read(buffer)) > 0) {
			os.write(buffer, 0, count);
		}
		os.close();
		is.close();

		/* 获取当前登录用户的用户名 */
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String image = request.getParameter("image");

		/* 获取表单数据 */
		String order_title = order.getOrder_title();
		String order_info = order.getOrder_info();
		String order_date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

		/* 新建表单局部变量，赋值，封面图片名称为文件上传里得到的，时间为系统自动生成 */
		OrderCreate order_add = new OrderCreate();
		order_add.setOrder_creater(username);
		order_add.setOrder_title(order_title);
		order_add.setOrder_info(order_info);
		order_add.setOrder_date(order_date);
		order_add.setLim("0");
		order_add.setOrder_img(order_img);

		/* 开启数据库查询事务 */
		HibCommons.OpenQuery();

		/* 写入数据 */
		HibCommons.getSession().save(order_add);

		/* 关闭数据库事务 */
		HibCommons.CloseQuery();

		/* 将用户名写入session */
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);

		return SUCCESS;
	}

	public String del_order() {
		/* 获取username及image及要删除的歌单id参数 */
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String image = request.getParameter("image");
		String id = request.getParameter("id");
		/* 开启数据库查询 */
		HibCommons.OpenQuery();
		/* 执行删除 */
		String hql = "DELETE FROM OrderCreate WHERE id = :id";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("id", id);
		query.executeUpdate();
		/* 关闭事务 */
		HibCommons.CloseQuery();
		/* 将username及image放入session */
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);

		return SUCCESS;
	}

	public String del_save_order() {
		/* 获取username及image及要删除的歌单id参数 */
		HttpServletRequest request = ServletActionContext.getRequest();
		String username = request.getParameter("username");
		String image = request.getParameter("image");
		String id = request.getParameter("id");
		System.out.println("haha" + username);
		System.out.println(image);
		System.out.println(id);
		/* 开启数据库查询 */
		HibCommons.OpenQuery();
		/* 执行删除 */
		String hql = "DELETE FROM OrderSave WHERE id = :id";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("id", id);
		query.executeUpdate();
		/* 关闭事务 */
		HibCommons.CloseQuery();
		/* 将username及image放入session */
		Map map = ActionContext.getContext().getSession();
		user.setUsername(username);
		user.setImage(image);
		map.put("username", user);
		return SUCCESS;
	}

	public OrderCreate getOrder() {
		return order;
	}

	public void setOrder(OrderCreate order) {
		this.order = order;
	}

	public List<OrderCreate> getAdmin_order() {
		return admin_order;
	}

	public void setAdmin_order(List<OrderCreate> admin_order) {
		this.admin_order = admin_order;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public File getOrder_add() {
		return order_add;
	}

	public void setOrder_add(File order_add) {
		this.order_add = order_add;
	}

	public String getOrder_addFileName() {
		return order_addFileName;
	}

	public void setOrder_addFileName(String order_addFileName) {
		this.order_addFileName = order_addFileName;
	}

	public List<OrderSave> getOrder_save() {
		return order_save;
	}

	public void setOrder_save(List<OrderSave> order_save) {
		this.order_save = order_save;
	}

	public OrderSave getOrder_s() {
		return order_s;
	}

	public void setOrder_s(OrderSave order_s) {
		this.order_s = order_s;
	}

	public int getPageNum_sc() {
		return pageNum_sc;
	}

	public void setPageNum_sc(int pageNum_sc) {
		this.pageNum_sc = pageNum_sc;
	}

	public int getTotalpage_sc() {
		return totalpage_sc;
	}

	public void setTotalpage_sc(int totalpage_sc) {
		this.totalpage_sc = totalpage_sc;
	}

	public int getPageSize_sc() {
		return pageSize_sc;
	}

	public void setPageSize_sc(int pageSize_sc) {
		this.pageSize_sc = pageSize_sc;
	}

	public int getAmount_sc(String username) {
		int i = 0;
		HibCommons.OpenQuery();
		String hql = "SELECT count(*) FROM OrderCreate o where o.order_creater = :username";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("username", username);
		// 2、获取结果（结果为long类型）
		Object obj = query.uniqueResult();
		// 3、转化为long(类型为long，转为int会报错)
		Long long1 = (Long) obj;
		i = long1.intValue();
		HibCommons.CloseQuery();
		
		System.out.println("i:"+i);
		return i;
	}

	public void setAmount_sc(int amount_sc) {
		Amount_sc = amount_sc;
	}

	public int getPageNum_ss() {
		return pageNum_ss;
	}

	public void setPageNum_ss(int pageNum_ss) {
		this.pageNum_ss = pageNum_ss;
	}

	public int getTotalpage_ss() {
		return totalpage_ss;
	}

	public void setTotalpage_ss(int totalpage_ss) {
		this.totalpage_ss = totalpage_ss;
	}

	public int getPageSize_ss() {
		return pageSize_ss;
	}

	public void setPageSize_ss(int pageSize_ss) {
		this.pageSize_ss = pageSize_ss;
	}

	public int getAmount_ss(String username) {
		int i = 0;
		HibCommons.OpenQuery();
		String hql = "SELECT count(*) FROM OrderSave o where o.order_saver = :username";
		Query query = HibCommons.getSession().createQuery(hql);
		query.setParameter("username", username);
		// 2、获取结果（结果为long类型）
		Object obj = query.uniqueResult();
		// 3、转化为long(类型为long，转为int会报错)
		Long long1 = (Long) obj;
		i = long1.intValue();
		HibCommons.CloseQuery();
		return i;
	}

	public void setAmount_ss(int amount_ss) {
		Amount_ss = amount_ss;
	}
	
	

}
